define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;

    function initData() {
        getNormalData(1, rows);
        getDeleteData(1, rows);
    }

    $(document).on('click', '.query-normal', function () {
        getNormalData(1, rows);
    });

    $(document).on('click', '.query-delete', function () {
        getDeleteData(1, rows);
    });

    $(document).on('click', '.check-all-normal', function () {
        if ($(this).is(":checked")) {
            $('input[name="normal-user-id"]').prop("checked", true);//全选
            $('.rowover-table tbody tr').addClass('tr-click');
        } else {
            $('input[name="normal-user-id"]').prop("checked", false);//取消全选
            $('.rowover-table tbody tr').removeClass('tr-click')
        }
    }).on('click', '.check-all-delete', function () {
        if ($(this).is(":checked")) {
            $('input[name="delete-user-id"]').prop("checked", true);//全选
        } else {
            $('input[name="delete-user-id"]').prop("checked", false);//取消全选
        }
    }).on('click', '.activate-user', function () {
        var $this = $(this);
        var name = $this.attr('data-name');
        $.confirm({
            body: '确定要启用' + name + '吗',
            okHidden: function () {
                var id = $this.attr('data-id');
                var uids = new Array();
                uids[0] = parseInt(id);
                $.post('/sysUser/activate', {uids: JSON.stringify(uids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('启用成功');
                            initData();
                            break;
                        default :
                            toastError('启用失败');
                    }
                });
            }
        });
    }).on('click', '.activate-all', function () {
        var inputs = $('input[name="delete-user-id"]');
        var uids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                uids[i] = parseInt($(item).val());
            }
        });
        if (uids.length == 0) {
            toastInfo('请选择要启用的账号');
            return;
        }
        $.confirm({
            body: '确定要启用这些账号吗',
            okHidden: function () {
                $.post('/sysUser/activate', {uids: JSON.stringify(uids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('启用成功');
                            initData();
                            break;
                        default :
                            toastError('启用失败');
                    }
                });

            }
        });
    }).on('click', '.dis-activate-user', function () {
        var $this = $(this);
        var name = $this.attr('data-name');
        $.confirm({
            body: '确定要注销' + name + '吗',
            okHidden: function () {
                var id = $this.attr('data-id');
                var uids = new Array();
                uids[0] = parseInt(id);
                $.post('/sysUser/disactivate', {uids: JSON.stringify(uids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('注销成功');
                            initData();
                            break;
                        default :
                            toastError('注销失败');
                    }
                });
            }
        });
    }).on('click', '.dis-activate-all', function () {
        var inputs = $('input[name="normal-user-id"]');
        var uids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                uids[i] = parseInt($(item).val());
            }
        });
        if (uids.length == 0) {
            toastInfo('请选择要注销的账号');
            return;
        }
        $.confirm({
            body: '确定要注销这些账号吗',
            okHidden: function () {
                $.post('/sysUser/disactivate', {uids: JSON.stringify(uids)}, function (data) {
                    switch (data.code) {
                        case 0:
                            toastSuccess('注销成功');
                            initData();
                            break;
                        default :
                            toastError('注销失败');
                    }
                });
            }
        });
    });

    function getNormalData(page, rows) {
        var nickName = $.trim($('#nickName1').val());
        var accountName = $.trim($('#accountName1').val());
        var data = {
            nickName: nickName,
            accountName: accountName,
            page: page,
            rows: rows,
            statusCode: '0'
        };
        $.post('/sysUser/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-normal', {list: list});
                    $('#tab-normal tbody').empty();
                    $('#tab-normal tbody').html(html);
                    $('.normal-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    function getDeleteData(page, rows) {
        var nickName = $.trim($('#nickName2').val());
        var accountName = $.trim($('#accountName2').val());
        var data = {
            nickName: nickName,
            accountName: accountName,
            page: page,
            rows: rows,
            statusCode: '1'
        };
        $.post('/sysUser/list', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-delete', {list: list});
                    $('#tab-delete tbody').empty();
                    $('#tab-delete tbody').html(html);
                    $('.delete-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.normal-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getNormalData(num, rows)
        }
    });

    $('.delete-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getDeleteData(num, rows)
        }
    })

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});